﻿using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using K9Nano.LayIM.Models;

namespace K9Nano.LayIM.Hubs
{
    public interface IChatHubContext
    {
        void Send(string to, string type, object data);

        Task SendAsync(string to, string type, object data);

        Task SendToGroupAsync(string group, string type, object data);

        Task SendToGroupAsync(string group, string type, object data, string excludedConnectionId);

        Task SendToGroupAsync(string group, string type, object data, IReadOnlyList<string> excludedConnectionIds);

        Task JoinHubGroupAsync(string group, string connectionId);

        void AddFriend(string userId, LayIMAddFriend friend)
        {
            Send(userId, "addList", friend);
        }

        Task AddFriendAsync(string userId, LayIMAddFriend friend)
        {
            return SendAsync(userId, "addList", friend);
        }

        void AddGroup(string userId, LayIMAddGroup group)
        {
            Send(userId, "addList", group);
        }

        Task AddGroupAsync(string userId, LayIMAddGroup group)
        {
            return SendAsync(userId, "addList", group);
        }

        Task NotifyFriendOnlineAsync(string userId, string friendId, CancellationToken cancellation)
        {
            return SendAsync(userId, "setFriendStatus", new { friendId, status = "online" });
        }

        Task NotifyFriendOfflineAsync(string userId, string friendId, CancellationToken cancellation)
        {
            return SendAsync(userId, "setFriendStatus", new { friendId, status = "offline" });
        }

        Task NotifyRoomUserOfflineAsync(string roomId, string userName, CancellationToken cancellation)
        {
            return SendToGroupAsync(roomId, "chat", new LayIMReceiveMessage
            {
                system = true,
                id = roomId,
                type = "group",
                content = $"{userName} 已下线"
            });
        }

        Task NotifyRoomUserOnlineAsync(string roomId, string userConnectionId, string userName, CancellationToken cancellation)
        {
            return SendToGroupAsync(roomId, "chat", new LayIMReceiveMessage
            {
                system = true,
                id = roomId,
                type = "group",
                content = $"{userName} 已上线"
            }, userConnectionId);
        }
    }
}